package com.vitabuybuy.tv.ui.mine.group

import android.os.Bundle
import android.view.View
import androidx.core.os.bundleOf
import androidx.lifecycle.Observer
import com.library.common.base.BaseActivity
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityMyGroupBuyDetailBinding
import com.vitabuybuy.tv.ui.mine.group.viewmodel.MyGroupVieModel
import com.vitabuybuy.tv.ui.mine.order.OrderDetailActivity
import kotlinx.android.synthetic.main.activity_my_group_buy_detail.*

class MyGroupBuyDetailActivity : BaseActivity<MyGroupVieModel, ActivityMyGroupBuyDetailBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_my_group_buy_detail

    private var type: Int? = 1
    private var groupId: Int? = 1

    override fun onGetBundle(bundle: Bundle?) {
        type = bundle?.getInt("type")
        groupId = bundle?.getInt("groupId")
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.group_buy_detail))
        mBinding.mViewModel = viewModel
    }

    override fun observeData() {
        viewModel.getMyGroupBuyDetail(groupId, type)
        viewModel.groupDetail.observe(this, Observer {
            tvStatus.text = when (it.status) {
                "participating" -> getString(R.string.participating)
                "gain" -> getString(R.string.won)
                "failure" -> {
                    if (type == 2) {
                        getString(R.string.missed)
                    } else {
                        getString(R.string.not_become_group)
                    }
                }
                "underway" -> getString(R.string.wait_become_group)
                "formed" -> getString(R.string.has_become_group)
                else -> ""
            }
            when (it.status) {
                "gain", "formed" -> {
                    btnCheckOrder.visibility = View.VISIBLE
                }
                else -> {
                    btnCheckOrder.visibility = View.GONE
                }
            }
        })
    }

    override fun eventHandling() {
        btnCheckOrder.setOnClickListener {
            viewModel.groupDetail.value?.run {
                if (this@MyGroupBuyDetailActivity.type == 2) {
                    OrderDetailActivity.openActivity(mContext, this.purchasingGroup.orderId)
                } else {
                    OrderDetailActivity.openActivity(mContext, this.orderId)
                }
            }
        }
    }


    companion object {
        fun openActivity(mContext: BaseActivity<*, *>, groupId: Int, type: Int) {
            mContext.startActivity(
                bundleOf(
                    Pair("groupId", groupId),
                    Pair("type", type)
                ), MyGroupBuyDetailActivity::class.java
            )
        }
    }
}
